package com.controls.ws;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;

public class GmailWSControl extends BaseWSControl {
	public GmailWSControl(Context cxt, WebServiceCommunicatorListener lsn) {
		super(cxt, lsn);
	}

	public boolean importGmailContacts(String accessToken, String gUsername,
			String gPassword) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<ImportGMailContacts xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>" + accessToken + "</Token>" + "<GMailUserName>"
				+ gUsername + "</GMailUserName>" + "<GMailPassword>"
				+ gPassword + "</GMailPassword>" + "</ImportGMailContacts>"
				+ "</soap:Body>" + "</soap:Envelope>";

		fetch(WebServiceFlag.IMPORT_GMAIL, content);
		return true;
	}

	public String parseImportGmail(String xml) {
		String returnValue = "";
		Document doc = getDomElement(xml);
		if (doc == null) {
			return returnValue;
		}

		NodeList results = doc
				.getElementsByTagName("ImportGMailContactsResult");
		JSONObject mObj = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mObj = new JSONObject(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mObj != null) {
			try {

				if (mObj.has("ReturnValue")) {
					returnValue = mObj.getString("ReturnValue");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnValue;
	}
}
